import {
  getAllResearch,
  getNewDoingList,
  getNewPersonMonthScoreList,
  getNewPersonYearScoreList,
  getNewTeamScoreList,
  getNewTeamYdjfRange,
  getNewToDoList
} from "@/api/home/total";
import { hasAuth } from "@/router/utils";
import { dealDataCut, exportData } from "@/utils/common";
import { onMounted, ref } from "vue";

import sumImg1 from "@/assets/home/sum1.png";
import sumImg2 from "@/assets/home/sum2.png";
import sumImg3 from "@/assets/home/sum3.png";
import sumImg4 from "@/assets/home/sum4.png";
import sumImg5 from "@/assets/home/sum5.png";
import sumImg6 from "@/assets/home/sum6.png";
import sumImg7 from "@/assets/home/sum7.png";
import dayjs from "dayjs";

export function useTotal() {
  const isCanDelAchiev = ref(false);
  const searchDate = ref([
    dayjs().subtract(1, "month").format("YYYY-MM-DD"),
    dayjs().format("YYYY-MM-DD")
  ]);
  const searchParams = ref<any>({});
  const grJfLoading = ref(false);
  const tdjfLoading = ref(false);
  const todoList = ref([
    { name: "论文", count: 0 },
    { name: "横向项目", count: 0 },
    { name: "纵向项目", count: 0 },
    { name: "专利", count: 0 },
    { name: "平台申报", count: 0 },
    { name: "成果转化", count: 0 }
  ]);
  const doingList = ref([
    { name: "论文", count: 0 },
    { name: "横向项目", count: 0 },
    { name: "纵向项目", count: 0 },
    { name: "专利", count: 0 },
    { name: "平台申报", count: 0 }
  ]);
  const wdkyList = ref([
    { name: "论文", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "专利", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "横向项目", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "纵向项目", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "成果转化", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "平台申报", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "科研获奖", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "科研成果", img: "", zs: 0, jxz: 0, ywc: 0 },
    { name: "成果转化", img: "", zs: 0, jxz: 0, ywc: 0 }
  ]);
  const grndList = ref([]);
  const curUserYearRank = ref<any>({});
  const curUserMonthRank = ref<any>({});
  const tdndList = ref([]);
  const grydList = ref([]);
  const tdkycgList = ref([]);
  const tdydjfList = ref([]);
  const achievList = ref([]);

  const getUserList = item => {
    let allUserList = [];
    let data = item.researchTeam;
    if (data) {
      if (data.masterUser) allUserList.push(data.masterUser);
      if (data.secretaryUser) allUserList.push(data.secretaryUser);
      if (data.userList && data.userList.length > 0) {
        allUserList = allUserList.concat(data.userList);
      }
    }
    return allUserList;
  };

  const searchTotalData = () => {
    // 代表事项
    getNewToDoList().then(res => {
      let todoData = res.data;
      let tempTodoList = [
        { name: "论文", count: todoData.statusPaperCount || 0 },
        {
          name: "横向项目",
          count: todoData.statusHorizontalProjectsCount || 0
        },
        {
          name: "纵向项目",
          count: todoData.statusLongitudinalProjectsCount || 0
        },
        { name: "专利", count: todoData.statusPatentsCount || 0 },
        {
          name: "平台申报",
          count: todoData.statusPlatformDeclarationCount || 0
        },
        {
          name: "成果转化",
          count: todoData.achievementTransformation || 0
        }
      ];
      todoList.value = tempTodoList;
    });

    // 项目进行中
    getNewDoingList().then(res => {
      let todoData = res.data;
      let tempDoingList = [
        { name: "论文", count: todoData.statusPaperCount || 0 },
        {
          name: "横向项目",
          count: todoData.statusHorizontalProjectsCount || 0
        },
        {
          name: "纵向项目",
          count: todoData.statusLongitudinalProjectsCount || 0
        },
        { name: "专利", count: todoData.statusPatentsCount || 0 },
        {
          name: "平台申报",
          count: todoData.statusPlatformDeclarationCount || 0
        }
      ];
      doingList.value = tempDoingList;
    });

    // 我的科研
    getAllResearch().then(res => {
      let data = res.data || {};
      let tempList = [
        {
          name: "论文",
          img: sumImg3,
          zs: data.paperCount || 0,
          jxz: data.statusPaperCount || 0,
          ywc: dealDataCut(data.paperCount, data.statusPaperCount) || 0
        },
        {
          name: "专利",
          img: sumImg4,
          zs: data.patentsCount || 0,
          jxz: data.statusPatentsCount || 0,
          ywc: dealDataCut(data.patentsCount, data.statusPatentsCount) || 0
        },
        {
          name: "横向项目",
          img: sumImg1,
          zs: data.horizontalProjectsCount || 0,
          jxz: data.statusHorizontalProjectsCount || 0,
          ywc:
            dealDataCut(
              data.horizontalProjectsCount,
              data.statusHorizontalProjectsCount
            ) || 0
        },
        {
          name: "纵向项目",
          img: sumImg2,
          zs: data.longitudinalProjectsCount || 0,
          jxz: data.statusLongitudinalProjectsCount || 0,
          ywc:
            dealDataCut(
              data.longitudinalProjectsCount,
              data.statusLongitudinalProjectsCount
            ) || 0
        },
        {
          name: "成果转化",
          img: sumImg7,
          zs: data.achievementTransformationCount || 0,
          jxz: data.statusAchievementTransformationCount || 0,
          ywc:
            dealDataCut(
              data.achievementTransformationCount,
              data.statusAchievementTransformationCount
            ) || 0
        },
        {
          name: "平台申报",
          img: sumImg5,
          zs: data.platformDeclarationCount || 0,
          jxz: data.statusPlatformDeclarationCount || 0,
          ywc:
            dealDataCut(
              data.platformDeclarationCount,
              data.statusPlatformDeclarationCount
            ) || 0
        },
        {
          name: "科研获奖",
          img: sumImg6,
          zs: data.otherObtainRewardCount || 0,
          jxz: data.statusOtherObtainRewardCount || 0,
          ywc:
            dealDataCut(
              data.otherObtainRewardCount,
              data.statusOtherObtainRewardCount
            ) || 0
        },
        {
          name: "科研成果",
          img: sumImg7,
          zs: data.scrAchievementsCount || 0,
          jxz: data.statusScrAchievementsCount || 0,
          ywc:
            dealDataCut(
              data.scrAchievementsCount,
              data.statusScrAchievementsCount
            ) || 0
        },
        {
          name: "教学成果",
          img: sumImg7,
          zs: data.teachingAchievementsCount || 0,
          jxz: data.statusTeachingAchievementsCount || 0,
          ywc:
            dealDataCut(
              data.teachingAchievementsCount,
              data.statusTeachingAchievementsCount
            ) || 0
        }
      ];
      wdkyList.value = tempList;
    });

    loadDataByYear();

    // 个人月度
    getNewPersonMonthScoreList({ limit: 999 }).then(res => {
      curUserMonthRank.value = res.data.curUser || {};
      grydList.value = res.data?.userPage || [];
    });

    // 团队月度
    getNewTeamYdjfRange({ pageNum: 1, pageSize: 999 }).then(res => {
      tdydjfList.value = res.data?.records || [];
    });
  };

  const loadDataByYear = () => {
    // 个人年度积分排名
    grJfLoading.value = true;
    let searchStartTime = searchDate.value[0] + " 00:00:00";
    let searchEndTime = searchDate.value[1] + " 23:59:59";
    let grParams = {
      limit: 999,
      startTime: searchStartTime,
      endTime: searchEndTime
    };
    searchParams.value = grParams;
    getNewPersonYearScoreList(grParams)
      .then(res => {
        curUserYearRank.value = res.data.curUser || {};
        grndList.value = res.data?.userPage || [];
      })
      .finally(() => {
        grJfLoading.value = false;
      });

    // 团队年度积分排名
    tdjfLoading.value = true;
    let tdParams = {
      startTime: searchStartTime,
      endTime: searchEndTime,
      pageNum: 1,
      pageSize: 999
    };
    getNewTeamScoreList(tdParams)
      .then(res => {
        tdndList.value = res.data?.records || [];
      })
      .finally(() => {
        tdjfLoading.value = false;
      });
  };

  // 导出数据
  const exportModuleData = () => {
    exportData(
      `全院年度积分排名(${searchDate.value[0]}~${searchDate.value[1]})`,
      "/index/new/user/score/range/page/year/export",
      searchParams.value
    );
  };

  onMounted(async () => {
    isCanDelAchiev.value = hasAuth("total:btn:delAchiev");
    searchTotalData();
  });

  return {
    isCanDelAchiev,
    searchDate,
    searchParams,
    grJfLoading,
    tdjfLoading,
    todoList,
    doingList,
    wdkyList,
    grndList,
    curUserYearRank,
    curUserMonthRank,
    tdndList,
    grydList,
    tdkycgList,
    tdydjfList,
    achievList,
    getUserList,
    loadDataByYear,
    exportModuleData
  };
}
